<template>
  <avue-form :option="option"
             v-model="form"
             @submit="submit">
  </avue-form>
</template>
<script>
import { mapGetters } from 'vuex'
import option from '@/const/cust/health/shfs'
import { findObj, saveObj } from '@/api/cust/health/health'
export default {
  data () {
    return {
      option: option,
      form: {
        jktjid: null,
        grjbxxid: null
      }
    }
  },
  watch: {
    'form.shfsyjqksfjj' () {
      this.colDisplay('shfsyjqksfjj', 'shfsyjqkjjnl', 2, '戒酒年龄')
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created () {
    this.form.grjbxxid = this.$route.query.grjbxxid
    this.form.jktjid = this.$route.params.id
    this.getTjb()
  },
  methods: {
    submit () {
      saveObj(this.form).then(data => {
        this.from = data.data
        this.$message({
          showClose: true,
          message: '保存成功',
          type: 'success'
        })
      })
    },
    getTjb () {
      findObj(this.form).then(res => {
        if (res.data.code == 0 && res.data.data != null) {
          this.form = res.data.data[0]
        }
        if (this.validatenull(this.form.shfsxyqkxyzk)) {
          this.form.shfsxyqkxyzk = '1'
        }
        if (this.validatenull(this.form.shfsyjqkyjpl)) {
          this.form.shfsyjqkyjpl = '1'
        }
        if (this.validatenull(this.form.zybwhysjcs)) {
          this.form.zybwhysjcs = '1'
        }
        if (this.validatenull(this.form.shfsysxg)) {
          this.form.shfsysxg = '1'
        }
      })
    },
    colDisplay (key1, key2, value, message) {
      var flag = false
      const des = this.findColumnIndex(key2)
      const desCol = this.option.group[des[0]].column[des[1]]
      const form = this.form[key1]
      flag = Array.isArray(form)
        ? form.includes(value)
        : this.form[key1] == value
      if (flag) {
        desCol.display = true
        desCol.rules = [
          {
            required: true,
            message: '请输入' + message
          }
        ]
      } else {
        desCol.display = false
        desCol.rules = []
      }
    },
    findColumnIndex (prop) {
      let formIndex = -1
      let groupIndex = 1
      this.option.group.forEach((ele, index) => {
        ele.column.forEach((cele, cindex) => {
          if (cele.prop === prop) {
            groupIndex = index
            formIndex = cindex
          }
        })
      })
      return [groupIndex, formIndex]
    }
  }
}
</script>